package amigos;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Traverser;

import daos.SessionManager;
import servicios.Operation;

public class GetTodosLosAmigos implements Operation<List<String>> {

	private Node startPoint;

	public GetTodosLosAmigos(String nombre) {
		IndexManager index = SessionManager.getGraphSession().index();
		Index<Node> forNodes = index.forNodes("users");
		this.startPoint = forNodes.query("UserName",nombre).getSingle();
	}
	
	private Traverser getAllMyFriends(final Node nodoInicial) {
		TraversalDescription td = SessionManager.getGraphSession().traversalDescription()
				.breadthFirst()
				.relationships(TipoRelacion.AMIGO, Direction.BOTH)
				.evaluator(Evaluators.excludeStartPosition());
		return td.traverse(nodoInicial);
	}

	public List<String> execute() {
		List<String> retList = new ArrayList<String>();
		Traverser traverser = getAllMyFriends(this.startPoint);
		for (Path path : traverser) {
			retList.add((String) path.endNode().getProperty("UserName"));
			
		}
		
		return retList;
	}
	
	
	
}
